#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using ZO.SmartCore.Helpers;
using ZO.SmartCore.Windows.Forms.OptionControls.Design;
using ZO.SmartCore.Windows.Forms.Properties;

#endregion

namespace ZO.SmartCore.Windows.Forms.OptionControls
{
    /// <summary>
    /// Provides a user interface for display the Options. 
    /// </summary>
    [ToolboxItem(true)]
    [ToolboxBitmap(typeof(TreeView))]
    [Designer(typeof(SmartTreeOptionDesigner))]
    public class SmartTreeOption : SmartUserControl
    {

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="SmartTreeOption"/> class.
        /// </summary>
        public SmartTreeOption()
        {
            InitializeComponent();

            this.Nodes = new SmartTreeOptionNodes(this.OptionTreeView.Nodes, this);


            // Activates double buffering
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true);

            //load fonts
            FontHelper.Load(FontResource.CalibriBold);

            //set fonts
            Font callibriBold = FontHelper.GetFont("Calibri", 14.25F, FontStyle.Bold);
            
            this.OptionTitle.Font = callibriBold;

        }
        #endregion

        #region Destructor

        #endregion

        #region Fields
        private Button DefaultButton;
        private Button ApplyButton;
        private Button OKButton;
        private Button CancelButton;
        internal TreeView OptionTreeView;
        private SmartPanel TreePanel;
        private SmartTitle OptionTitle;
        internal Panel OptionPanel;
        private Panel ButtonPanel;
        private SmartTreeOptionNodes _Nodes;

        #endregion

        #region Events
        /// <summary>
        /// Raised When Apply Button is Clicked
        /// </summary>
        public event EventHandler ApplyClick;

        /// <summary>
        /// Raised When Default Button is Clicked
        /// </summary>
        public event EventHandler DefaultClick;

        /// <summary>
        /// Raised When OK Button is Clicked
        /// </summary>
        public event EventHandler OKClick;

        /// <summary>
        /// Raised When Cancel Button is Clicked
        /// </summary>
        public event EventHandler CancelClick;
        #endregion

        #region Operators

        #endregion

        #region Properties


        /// <summary>
        /// Gets or sets the text associated with this control.
        /// </summary>
        /// <value></value>
        /// <returns>The text associated with this control.</returns>
        [Browsable(true)]
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
            }
        }


        /// <summary>
        /// Gets the pages of tree control.
        /// </summary>
        /// <value>The pages.</value>
        public SmartTreeOptionNodes Nodes
        {
            get { return _Nodes; }
            set { _Nodes = value; }
        }
        #endregion



        #region Methods
        /// <summary>
        /// Initializes the component.
        /// </summary>
        private void InitializeComponent()
        {
            this.ButtonPanel = new Panel();
            this.ApplyButton = new Button();
            this.OKButton = new Button();
            this.CancelButton = new Button();
            this.DefaultButton = new Button();
            this.OptionTreeView = new TreeView();
            this.TreePanel = new SmartPanel();
            this.OptionPanel = new Panel();
            this.OptionTitle = new SmartTitle();
            this.ButtonPanel.SuspendLayout();
            this.TreePanel.SuspendLayout();
            this.SuspendLayout();
            // 
            // ButtonPanel
            // 
            this.ButtonPanel.Controls.Add(this.ApplyButton);
            this.ButtonPanel.Controls.Add(this.OKButton);
            this.ButtonPanel.Controls.Add(this.CancelButton);
            this.ButtonPanel.Controls.Add(this.DefaultButton);
            this.ButtonPanel.Dock = DockStyle.Bottom;
            this.ButtonPanel.Location = new Point(0, 363);
            this.ButtonPanel.Name = "ButtonPanel";
            this.ButtonPanel.Size = new Size(618, 40);
            this.ButtonPanel.TabIndex = 0;
            // 
            // ApplyButton
            // 
            this.ApplyButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
            this.ApplyButton.FlatStyle = FlatStyle.System;
            this.ApplyButton.Font = new Font("Tahoma", 8.25F, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.ApplyButton.Location = new Point(534, 9);
            this.ApplyButton.Name = "ApplyButton";
            this.ApplyButton.Size = new Size(72, 22);
            this.ApplyButton.TabIndex = 8;
            this.ApplyButton.Text = "&Apply";
            this.ApplyButton.UseVisualStyleBackColor = true;
            this.ApplyButton.Click += new EventHandler(this.OnApplyClick);
            // 
            // OKButton
            // 
            this.OKButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
            this.OKButton.FlatStyle = FlatStyle.System;
            this.OKButton.Font = new Font("Tahoma", 8.25F, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.OKButton.Location = new Point(366, 9);
            this.OKButton.Name = "OKButton";
            this.OKButton.Size = new Size(76, 22);
            this.OKButton.TabIndex = 6;
            this.OKButton.Text = "&OK";
            this.OKButton.UseVisualStyleBackColor = true;
            this.OKButton.Click += new EventHandler(this.OnOKClick);
            // 
            // CancelButton
            // 
            this.CancelButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
            this.CancelButton.FlatStyle = FlatStyle.System;
            this.CancelButton.Font = new Font("Tahoma", 8.25F, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.CancelButton.Location = new Point(450, 9);
            this.CancelButton.Name = "CancelButton";
            this.CancelButton.Size = new Size(76, 22);
            this.CancelButton.TabIndex = 7;
            this.CancelButton.Text = "&Cancel";
            this.CancelButton.UseVisualStyleBackColor = true;
            this.CancelButton.Click += new EventHandler(this.OnCancelClick);
            // 
            // DefaultButton
            // 
            this.DefaultButton.FlatStyle = FlatStyle.System;
            this.DefaultButton.Font = new Font("Tahoma", 8.25F, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.DefaultButton.Location = new Point(10, 9);
            this.DefaultButton.Name = "DefaultButton";
            this.DefaultButton.Size = new Size(76, 22);
            this.DefaultButton.TabIndex = 3;
            this.DefaultButton.Text = "&Default";
            this.DefaultButton.UseVisualStyleBackColor = true;
            this.DefaultButton.Visible = false;
            this.DefaultButton.Click += new EventHandler(this.OnDefaultClick);
            // 
            // OptionTreeView
            // 
            this.OptionTreeView.Location = new Point(7, 9);
            this.OptionTreeView.Name = "OptionTreeView";
            this.OptionTreeView.Size = new Size(165, 354);
            this.OptionTreeView.TabIndex = 2;
            this.OptionTreeView.AfterSelect += new TreeViewEventHandler(this.OptionTreeView_AfterSelect);
            // 
            // TreePanel
            // 
            this.TreePanel.BorderColor.Bottom = SystemColors.ButtonShadow;
            this.TreePanel.BorderColor.Left = SystemColors.ButtonShadow;
            this.TreePanel.BorderColor.Right = SystemColors.ButtonShadow;
            this.TreePanel.BorderColor.Top = SystemColors.ButtonShadow;
            this.TreePanel.BorderStyle = Forms.BorderStyle.Solid;
            this.TreePanel.BorderWidth = new Padding(1);
            this.TreePanel.Controls.Add(this.OptionPanel);
            this.TreePanel.Controls.Add(this.OptionTitle);
            this.TreePanel.Location = new Point(178, 10);
            this.TreePanel.Name = "TreePanel";
            this.TreePanel.Padding = new Padding(1);
            this.TreePanel.Size = new Size(432, 354);
            this.TreePanel.TabIndex = 3;
            this.TreePanel.Transparent = false;
            // 
            // OptionPanel
            // 
            this.OptionPanel.BackColor = SystemColors.Window;
            this.OptionPanel.Dock = DockStyle.Fill;
            this.OptionPanel.Location = new Point(1, 31);
            this.OptionPanel.Name = "OptionPanel";
            this.OptionPanel.Size = new Size(430, 322);
            this.OptionPanel.TabIndex = 1;
            // 
            // OptionTitle
            // 
            this.OptionTitle.BorderColor.Bottom = SystemColors.ButtonShadow;
            this.OptionTitle.BorderColor.Left = SystemColors.ButtonShadow;
            this.OptionTitle.BorderColor.Right = SystemColors.ButtonShadow;
            this.OptionTitle.BorderColor.Top = SystemColors.ButtonShadow;
            this.OptionTitle.BorderStyle = Forms.BorderStyle.None;
            this.OptionTitle.BorderWidth = new Padding(1);
            this.OptionTitle.Color1 = SystemColors.GradientInactiveCaption;
            this.OptionTitle.Color2 = SystemColors.InactiveCaptionText;
            this.OptionTitle.Dock = DockStyle.Top;
            this.OptionTitle.Font = new Font("Verdana", 11.25F, FontStyle.Bold, GraphicsUnit.Point, 0);
            this.OptionTitle.ForeColor = SystemColors.InactiveCaptionText;
            this.OptionTitle.Location = new Point(1, 1);
            this.OptionTitle.Name = "OptionTitle";
            this.OptionTitle.Orientation = Orientation.Horizontal;
            this.OptionTitle.Size = new Size(430, 30);
            this.OptionTitle.TabIndex = 0;
            this.OptionTitle.Text = "Option Title";
            this.OptionTitle.TextAlign = ContentAlignment.MiddleLeft;
            this.OptionTitle.TextGap = new Padding(4, 2, 2, 2);
            // 
            // TreeOption
            // 
            this.Controls.Add(this.TreePanel);
            this.Controls.Add(this.OptionTreeView);
            this.Controls.Add(this.ButtonPanel);
            this.DoubleBuffered = true;
            this.ForeColor = SystemColors.HotTrack;
            this.MinimumSize = new Size(618, 403);
            this.Name = "TreeOption";
            this.Size = new Size(618, 403);
            this.ButtonPanel.ResumeLayout(false);
            this.TreePanel.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        /// <summary>
        /// Raises the resize event.
        /// </summary>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected override void OnResize(EventArgs e)
        {
            int height = this.ClientSize.Height - this.ButtonPanel.Height - 9;
            this.OptionTreeView.Size = new Size(165, height);
            this.OptionTreeView.Location = new Point(7, 9);

            int panelLeft = this.OptionTreeView.Width + 7 + 6;
            int panelWidth = this.ClientSize.Width - panelLeft - 7;
            this.TreePanel.Location = new Point(panelLeft, 9);
            this.TreePanel.Size = new Size(panelWidth, height);
            this.TreePanel.Invalidate();
            //this.TopLabel.Refresh();
            base.OnResize(e);
        }
        #endregion

        /// <summary>
        /// Handles the AfterSelect event of the OptionTreeView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.Windows.Forms.TreeViewEventArgs"/> instance containing the event data.</param>
        private void OptionTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node is SmartTreeOptionNode)
            {
                //removes any controls
                if (this.OptionPanel.Controls.Count > 0)
                {
                    OptionPanel.Controls.RemoveAt(0);
                }
                SmartTreeOptionNode node = e.Node as SmartTreeOptionNode;
                SmartOptionPage control = node.OptionPage;

                //add the current control to the main panel

                //if (control.AutoResize)
                //{
                //    this.ResizeIfNeeded(control);
                //}
                //

                if (control != null)
                {
                    OptionPanel.Controls.Add(control);
                }

                this.DefaultButton.Visible = (control == null) ? false : (control.CanRestoreDefaults ? true : false);

                //set the name of the control
                this.OptionTitle.Text = (control == null) ? this.Text : control.Text;
            }
        }

        /// <summary>
        /// Handles the Click event of the DefaultButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected virtual void OnDefaultClick(object sender, EventArgs e)
        {
            if (this.DefaultClick != null)
            {
                this.DefaultClick(this, EventArgs.Empty);
            }

        }

        /// <summary>
        /// Handles the Click event of the OKButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected virtual void OnOKClick(object sender, EventArgs e)
        {
            if (this.OKClick != null)
            {
                this.OKClick(this, EventArgs.Empty);
            }

        }

        /// <summary>
        /// Handles the Click event of the CancelButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected virtual void OnCancelClick(object sender, EventArgs e)
        {
            if (this.CancelClick != null)
            {
                this.CancelClick(this, EventArgs.Empty);
            }

        }

        /// <summary>
        /// Handles the Click event of the ApplyButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected virtual void OnApplyClick(object sender, EventArgs e)
        {
            if (this.ApplyClick != null)
            {
                this.ApplyClick(this, EventArgs.Empty);
            }

        }
    }
}
